﻿AddUserProfile = function()
{
    var count = $id("hf.upcount").innerHTML;
    var load = $id("load_UserProfile");
    
    StartLoadingAnimation(load);
    
    cbkUserProfile_c.PerformCallback("add" + DELIMITER + count);
}

UserProfile_OnClick = function(){
    var load = $id("load_UserProfile");
    var mode = trim($id("lnkedit_UserProfile").innerHTML);

    StartLoadingAnimation(load);
    
    if(mode == "save")
    {
        if(confirm("save changes?"))
        {
            // di sini kirim data perubahannya
            var input = getUserProfileData();
            if(input)
            {
                cbkUserProfile_c.PerformCallback(mode + DELIMITER + input);
            }
            else
            {
                EndLoadingAnimation(load);
                return;
            }
        }
        else
        {
            EndLoadingAnimation(load);
            return;
        }
    }
    else if(mode == "edit")
    {
        cbkUserProfile_c.PerformCallback(mode);
    }
}

cbkUserProfile_c_CallbackComplete = function(e){
    var div = $id("div_UserProfile");
    var load = $id("load_UserProfile");
    var lnk = $id("lnkedit_UserProfile");
    var dscd = $id("lnkdscd_UserProfile");
    var add = $id("lnkadd_UserProfile");

    EndLoadingAnimation(load);
    
    var mode = e.parameter.split(DELIMITER)[0];
    
    if(mode == "edit")
    {
        lnk.innerHTML = "save";
        dscd.style.display = "block";
        add.style.display = "block";
        div.innerHTML = e.result;
    }
    else if(mode == "add")
    {
        if( div.insertAdjacentHTML )
        {
            div.insertAdjacentHTML( "beforeEnd", e.result );
        }
        else
        {
            var div2 = document.createElement('div');
            div2.innerHTML = e.result;
            div.appendChild(div2);
        }
        
        var hf = $id("hf.upcount");
        if( hf.innerHTML != "" )
        {
            hf.innerHTML = parseInt(hf.innerHTML) + 1; 
        }
        else
        {
            hf.innerHTML = "1"; 
        }
    }
    else if(mode == "profpic_popup")
    {
        var div = $id("popuplayer");
        div.innerHTML = e.result;
    }
    else if(mode == "profpic_save")
    {
        var div = $id("popuplayer");
        div.innerHTML = "";
        div.style.display = "none";    }
    else
    {
        lnk.innerHTML = "edit";
        dscd.style.display = "none";
        add.style.display = "none";
        div.innerHTML = e.result;
    }
}
UserProfile_Discard = function(){
    if(confirm("discard changes?"))
    {
        var load = $id("load_UserProfile");
        
        StartLoadingAnimation(load);
        
        cbkUserProfile_c.PerformCallback("discard");
    }
    else
    {
        EndLoadingAnimation(load);
        return;
    }
}

closeEditor = function(){
    window.location.reload(true);}

toggle = function (fsid)
{
    var div = $id("div_" + fsid);
    var ld = $id("lnkdscd_" + fsid);
    var le = $id("lnkedit_" + fsid);
    
    var display = "";
    
    if( div.style.display != "none" )
    {
        display = "none";
    }
    
    div.style.display = display;
    
    if( le != null )
    {
        le.style.display = display;
    }
    
    if( ld == null )
    {
        return;
    }
    
    if( trim(le.innerHTML) == "edit" )
    {
        ld.style.display = "none";
    }
    else
    {
        ld.style.display = display;
    }
}

changeProfilePicture = function()
{
    var div = $id("popuplayer");
    div.style.display = "block";    div.innerHTML = "<img src='" + LOADING.src + "' alt='' />";    renderFirst();
    
    cbkUserProfile_c.PerformCallback("profpic_popup");
}

saveProfilePicture = function()
{
    var div = $id("popuplayer");
    div.style.display = "block";    div.innerHTML = "<img src='" + LOADING.src + "' alt='' />";    renderFirst();
    
    cbkUserProfile_c.PerformCallback("profpic_save");
}

getUserProfileData = function()
{
    //var inputs = $nm("txt");
    var divBI = $id("div_UserProfile");
    var inputs = $nm("txt", divBI, "input");
    var output = "";
    var hashArray = new Array();
    var iDuplicateCount = 0;
    var iNotUploadedCount = 0;
    var iEmptyTypeCount = 0;
    var iEmptyValueCount = 0;
    var iInvalidCount = 0;
    
    for( var i=0; i < inputs.length; i++ )
    {
        if( i > 0 )
        {
            output += DELIMITER;
        }
        
        var item = inputs[i];
        var lov = $id("lov_" + item.id);
        
        if( lov == null )
        {
            output += item.id + NEWLINE + item.value;
        }
        else
        {
            var lovh = $id("lovh_" + item.id);
            lov.style.backgroundColor = item.style.backgroundColor = "#FFFFFF";
            
            if(hashArray[lovh.value] != null)
            {
                lov.style.backgroundColor = "#FFAAAA";
                iDuplicateCount++;
            }
            
            var uplbtn = $id("uplbtn_" + item.id);
            
            if( uplbtn != null && item.value != "" )
            {
                item.style.backgroundColor = "#FFAAAA";
                iNotUploadedCount++;
            }
            
            if( lovh.value != "" && item.value != "" )
            {
                hashArray[lovh.value] = item.value;
                output += lov.value + NEWLINE + lovh.value + NEWLINE + item.value;        
            }
            else if ( lovh.value != "" )
            {
                iEmptyValueCount++;
                item.style.backgroundColor = "#FFAAAA";
            }
            else if ( item.value != "" )
            {
                iEmptyTypeCount++;
                lov.style.backgroundColor = "#FFAAAA";
            }
            
            if( lov.style.backgroundColor == "#FFAAAA" || item.style.backgroundColor == "#FFAAAA" )
            {
                iInvalidCount++;
            }
            
        }
    }
    
    var sxError = "";
    
    if( iDuplicateCount > 0 )
    {
        sxError += "terdapat jenis info yang sama! ";
    }
    
    if( iNotUploadedCount > 0 )
    {
        sxError += "terdapat file yang belum diupload! ";
    }
    
    if( iEmptyTypeCount > 0 )
    {
        sxError += "terdapat field jenis info yang belum diisi! ";
    }
    
    if( iEmptyValueCount > 0 )
    {
        sxError += "terdapat field isian info yang belum diisi! ";
    }
    
    if( iInvalidCount > 0 )
    {
        sxError += "terdapat pengisian info yang tidak sesuai! ";
    }
    
    if( sxError != "" )
    {
        alert(sxError);
        return false;
    }
    
    return output;
}

StartLoadingAnimation = function(div){
    div.innerHTML = "<img src='" + LOADING.src + "' alt='' />";    
}

EndLoadingAnimation = function(div){
    div.innerHTML = "";    
}